﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Datalayer;
using Entities;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace QLHoNgheo
{
    public partial class frm_HoNgheo : Form
    {
        public frm_HoNgheo()
        {
            InitializeComponent();
        }

        #region Biến
        IQueryMgr iq;
        DataTable dt;
        String _IDHoNgheo = null;
        DataRow r;
        bool _NewThanhVien = false;
        int ThanhVienID = 0;
        #endregion

        private void frm_HoNgheo_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            load_lk();
            xtraTab_search.PageVisible = true;
            cl_grvLookupedit.rpcb_TinhTrangViecLam(ref repositoryItemLookUp_ttvl);
        }
        
        private void load_tab_deail()
        {
            if (_IDHoNgheo != null)
            {
                load_dataedit();
                load_dgv(_IDHoNgheo);
                fill_GPHT(_IDHoNgheo);
                fill_ChiTieu_hgd(_IDHoNgheo);
                fill_Thunhap_hgd(_IDHoNgheo);
                fill_TaiSan_hgd(_IDHoNgheo);
                set_checklist_TTKhac();
                txt_maho.Enabled = false;
            }
            load_lkgrv();
            load_lk1();
            load_pane1_them();
        }

        #region Tab Search hộ nghèo

        private void bt_f5_search_Click(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            List<HoNgheo> ls = new List<HoNgheo>();
            if (lk_quan.EditValue != null)
            {
                int id = int.Parse(lk_quan.EditValue.ToString());
                ls = iq.HN_GetHoNgheoByQuanID(id);
                if (lk_phuong.EditValue != null)
                {
                    int idp = int.Parse(lk_phuong.EditValue.ToString());
                    ls = iq.HN_GetHoNgheoByPhuongID(idp);
                }
            }
            gridControl1.DataSource = cl_EditAll1Form1.Create_dt(ls);
        }
        
        private void bt_search_Click(object sender, EventArgs e)
        {
            decimal _Deci_search = decimal.Parse( txt_search.Text);
            iq = QueryMgr.GetDbInstance();
            List<HoNgheo> ls = new List<HoNgheo>() ;
            if (lk_quan.EditValue != null)
            {
                int id = int.Parse(lk_quan.EditValue.ToString());
                ls = iq.HN_GetHoNgheoByQuanID(id);
                if (lk_phuong.EditValue != null)
                {
                    int idp = int.Parse(lk_phuong.EditValue.ToString());
                    ls = iq.HN_GetHoNgheoByPhuongID(idp);
                }
            }
            else
            {
                ls = iq.HN_GetAllHoNgheo();
            }

            gridControl1.DataSource = cl_EditAll1Form1.Create_dt(ls,_Deci_search);
        }

        void load_lk()
        {
            cl_Lookupedit.Lucb_Quanhuyen(ref lk_quan);
        }

        private void lk_quan_EditValueChanged(object sender, EventArgs e)
        {
            int id = int.Parse(lk_quan.EditValue.ToString());
            iq = QueryMgr.GetDbInstance();
            List<HoNgheo> ls= iq.HN_GetHoNgheoByQuanID(id);

            gridControl1.DataSource = cl_EditAll1Form1.Create_dt(ls);
            cl_Lookupedit.lucb_PhuongXa(ref lk_phuong, id);
        }

        private void lk_phuong_EditValueChanged(object sender, EventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            int id = int.Parse(lk_phuong.EditValue.ToString());
            List<HoNgheo> ls = iq.HN_GetHoNgheoByPhuongID(id);

            gridControl1.DataSource = cl_EditAll1Form1.Create_dt(ls);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DataRow r;
            DevExpress.Utils.WaitDialogForm wd;
            wd = new DevExpress.Utils.WaitDialogForm("Vui lòng chờ trong giây lát.....", "Đang nạp dữ liệu");
            r = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (r != null)
            {
                //neededit
                string _ID = r["Ma_HoNgheo"].ToString();
                _IDHoNgheo = _ID;
                xtraTab_detail.PageVisible = true;
                xtraTab_search.PageVisible = false;
                load_tab_deail();
            }
            wd.Close();
        }

        private void bt_new_hongheo_Click(object sender, EventArgs e)
        {
            
            DevExpress.Utils.WaitDialogForm wd;
            wd = new DevExpress.Utils.WaitDialogForm("Vui lòng chờ trong giây lát.....", "Đang nạp dữ liệu");

            txt_ghichu.Text = "";
            txt_maho.Text = "";
            txt_maho.Enabled = true;
            txt_sonha.Text = "";
            lk1_quan.EditValue = null;
            lk1_phuong.EditValue = null;

            gridControl2.DataSource = null;
            //neededit
            _IDHoNgheo = null;
            xtraTab_detail.PageVisible = true;
            xtraTab_search.PageVisible = false;
            load_tab_deail();
            wd.Close();
        }
        #endregion

        #region Load Dữ liệu HỘ NGHÈO

        private void bt1_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                iq = QueryMgr.GetDbInstance();
                if (MessageBox.Show("Bạn muốn xóa thành viên này?", "Thực hiên chức năng xóa",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int ID = int.Parse(gridView2.GetFocusedRowCellValue("Ma_ChiTiet").ToString());
                    iq.Qhgd_DelOrResQuanHeGiaDinh(ID);
                    MessageBox.Show("Xóa thành viên thành công");
                    _NewThanhVien = false;
                    load_dgv(txt_maho.Text);
                    XulyButton1();
                }
            }
            catch { }
        }

        private void gridView2_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            r = gridView2.GetDataRow(gridView2.FocusedRowHandle);
            _NewThanhVien = true;
            if (r != null)
            {
                _NewThanhVien = false;
                QuanHeGiaDinh gd = new QuanHeGiaDinh();
                int index = e.FocusedRowHandle;
                ThanhVienID = int.Parse(r["Ma_ChiTiet"].ToString());
                iq = QueryMgr.GetDbInstance();
                gd = iq.QHGD_GetQuanHeGiaDinhByID(ThanhVienID);

                txt1_namsinh.Text = gd.NamSinh + "";
                txt1_tenchuho.Text = gd.Ten_ThanhVien;
                lk1_baotroxh.EditValue = gd.Ma_BaoTroXaHoi;
                lk1_capdanghoc.EditValue = gd.Ma_CapHoc;
                lk1_chinhsach.EditValue = gd.Ma_ChinhSach;
                lk1_dantoc.EditValue = gd.Ma_DanToc;
                lk1_quanhevschuho.EditValue = gd.Ma_QuanHe;
                lk1_tinhtrangcongviec.EditValue = gd.Ma_ViecLam;

                string gioitinh = r["GioiTinh"].ToString();
                if (gioitinh == "True")
                {
                    list_gioitinh.SelectedIndex = 0;
                }
                else
                    list_gioitinh.SelectedIndex = 1;

            }
        }

        void load_dataedit()
        {
            if (_IDHoNgheo != null)
            {
                HoNgheo hn = new HoNgheo();
                iq = QueryMgr.GetDbInstance();
                hn = iq.HN_GetHoNgheoByID(_IDHoNgheo);
                txt_ghichu.Text = hn.ghiChu;
                txt_maho.Text = _IDHoNgheo;
                txt_sonha.Text = hn.SoNha;
                lk1_quan.EditValue = hn.PhuongXa.Ma_QuanHuyen;
                lk1_phuong.EditValue = hn.Ma_Phuong;
            }
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            bool ok = true;
            iq = QueryMgr.GetDbInstance();
            HoNgheo hn = new HoNgheo();
            hn.Ma_HoNgheo = cl_Lookupedit.BatLoi( txt_maho,"Chưa nhập mã hộ",ref ok);
            hn.SoNha = cl_Lookupedit.BatLoi(txt_sonha,"Chưa nhập số nhà",ref ok);
            cl_Lookupedit.BatLoi( lk1_phuong,"Chưa chọn phường",ref ok);
            hn.ghiChu = txt_ghichu.Text;
            hn.IsDeleted = false;
            // kiểm tra có lỗi hay ko
            if (ok == true)
            {
                hn.Ma_Phuong = int.Parse(lk1_phuong.EditValue.ToString());
                if (_IDHoNgheo == null)//new
                {
                    //kiểm tra có tồn tại 1 mã hộ nghèo
                    if (iq.HN_GetHoNgheoByID(hn.Ma_HoNgheo) == null)
                    {
                        iq.Hn_NewHoNgheo(hn);

                        cl_Insert.ChiTiet_Giaiphaphotro_ins(txt_maho.Text);
                        cl_Insert.ChiTiet_ChiTieuHGD_ins(txt_maho.Text);
                        cl_Insert.ChiTiet_ThuNhapHoGD_ins(txt_maho.Text);
                        cl_Insert.ChiTiet_TaiSanHGD_ins(txt_maho.Text);
                        MessageBox.Show("Thêm hộ nghèo mới thành công !");
                        txt_maho.Enabled = false;
                        bt1_them_Click(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Đã tồn tại mã hộ nghèo này ! xin kiểm tra lại");
                    }
                }
                else // edit
                {
                    iq.Hn_EditHoNgheo(hn);
                    MessageBox.Show("Sửa thông tin hộ nghèo thành công !");
                }
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            xtraTab_detail.PageVisible = false;
            xtraTab_search.PageVisible = true;
        }

        #endregion

        #region tab THÀNH VIÊN TRONG GIA ĐÌNH
        private void bt1_them_Click(object sender, EventArgs e)
        {
            txt1_namsinh.Text = "";
            txt1_tenchuho.Text = "";
            lk1_baotroxh.EditValue = null;
            lk1_capdanghoc.EditValue = null;
            lk1_chinhsach.EditValue = null;
            lk1_dantoc.EditValue = null;
            lk1_quanhevschuho.EditValue = null;
            lk1_tinhtrangcongviec.EditValue = null;
            list_gioitinh.SelectedIndex = 0;
            _NewThanhVien = true;
            load_dgv(_IDHoNgheo);
            XulyButton1();
        }

        private void bt_del_hongheo_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn muốn xóa hộ nghèo này?", "Thực hiên chức năng xóa",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                iq = QueryMgr.GetDbInstance();
                if (iq.HN_GetHoNgheoByID(txt_maho.Text) != null)
                {
                    iq.Hn_DelOrResHoNgheo(txt_maho.Text);
                    MessageBox.Show("Xóa hộ nghèo : " + txt_maho.Text + " Thành công");
                }
                xtraTab_detail.PageVisible = false;
                xtraTab_search.PageVisible = true;
                gridControl1.Refresh();
            }
        }

        private void bt_refesh_Click(object sender, EventArgs e)
        {
            txt1_namsinh.Text = "";
            txt1_tenchuho.Text = "";
            lk1_baotroxh.EditValue = null;
            lk1_capdanghoc.EditValue = null;
            lk1_chinhsach.EditValue = null;
            lk1_dantoc.EditValue = null;
            lk1_quanhevschuho.EditValue = null;
            lk1_tinhtrangcongviec.EditValue = null;
            list_gioitinh.SelectedIndex = 0;
            _NewThanhVien = true;
            load_dgv(_IDHoNgheo);
        }

        private void bt1_saveall_Click(object sender, EventArgs e)
        {
            QuanHeGiaDinh gd = new QuanHeGiaDinh();
            if (BatLoi_QHGD())
            {
                iq = QueryMgr.GetDbInstance();
                gd = iq.QHGD_GetQuanHeGiaDinhByIsChuHo_HoNgheoID(txt_maho.Text);
                string idget = lk1_quanhevschuho.EditValue.ToString();
                if (gd != null && idget == "1")
                {
                    MessageBox.Show("Lỗi : Không thể tồn tại 2 thành viên là chủ hộ");
                    lk1_quanhevschuho.EditValue = "2";
                }
                else
                {
                    if (_NewThanhVien == true)
                    {
                        HoNgheo nh = new HoNgheo();
                        nh = iq.HN_GetHoNgheoByID(txt_maho.Text);
                        if (nh != null)
                        {
                            if (BatLoi_QHGD())
                            {
                                iq.Qhgd_NewQuanHeGiaDinh(Fill_QuanHeGiaDinh(0));
                                MessageBox.Show("Thêm 1 thành viên thành công");
                            }
                        }
                        else
                        {
                            MessageBox.Show("Chưa 'Lưu' hộ nghèo không thể thêm mới thành viên");
                        }
                    }
                    else
                    {
                        if (BatLoi_QHGD())
                        {
                            iq.Qhgd_EditQuanHeGiaDinh(Fill_QuanHeGiaDinh(ThanhVienID));
                            MessageBox.Show("Thay đổi thông tin 1 thành viên thành công");
                        }
                    }
                    _NewThanhVien = false;
                    load_dgv(txt_maho.Text);
                    XulyButton1();
                }
            }
        }

        private void lk1_quan_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(lk1_quan.EditValue.ToString());
                cl_Lookupedit.lucb_PhuongXa(ref lk1_phuong, id);
            }catch{}
        }

        private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            if (e.Button.ButtonType == NavigatorButtonType.Remove)
            {
                if (MessageBox.Show("Bạn muốn xóa thành viên này?", "Thực hiên chức năng xóa",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    e.Handled = true;

                }
                else
                {
                    int ID = int.Parse(gridView2.GetFocusedRowCellValue("Ma_ChiTiet").ToString());
                    iq.Qhgd_DelOrResQuanHeGiaDinh(ID);
                }
            }
            if (e.Button.ButtonType == NavigatorButtonType.EndEdit)
            {

            }
        }

        void load_pane1_them()
        {
            cl_Lookupedit.lucb_CapDangTheoHoc(ref lk1_capdanghoc);
            cl_Lookupedit.lucb_Dantoc(ref lk1_dantoc);
            cl_Lookupedit.lucb_DoiTuongBaoTroXaHoi(ref lk1_baotroxh);
            cl_Lookupedit.lucb_TinhTrangViecLam(ref lk1_tinhtrangcongviec);
            cl_Lookupedit.lucb_Quanhechuho(ref lk1_quanhevschuho);
            cl_Lookupedit.lucb_ChinhSach(ref lk1_chinhsach);
        }

        void load_dgv(string IDHongheo)
        {
            iq = QueryMgr.GetDbInstance();
            dt = new DataTable();
            dt = Cl_Convert.ToDataTable(iq.QHGD_GetQuanHeGiaDinhByHoNgheoID(IDHongheo));
            gridControl2.DataSource = dt;
            if (dt.Rows.Count == 0)
            {
                _NewThanhVien = true;
                XulyButton1();
            }
        }

        void load_lkgrv()
        {
            repositoryItemImageComboBox1.Items.Clear();
            repositoryItemImageComboBox1.SmallImages = imageList1;
            repositoryItemImageComboBox1.Items.Add(new ImageComboBoxItem("Nam", true, 1));
            repositoryItemImageComboBox1.Items.Add(new ImageComboBoxItem("Nữ", false, 0));

            cl_grvLookupedit.rpcb_Quanhechuho(ref rplk_quanhechuho);
            cl_grvLookupedit.rpcb_CapDangTheoHoc(ref rpilk_capdanghoc);
            cl_grvLookupedit.rpcb_Dantoc(ref rpilk_dantoc);
            cl_grvLookupedit.rpcb_DoiTuongBaoTroXaHoi(ref rpilk_btxh);
            cl_grvLookupedit.rpcb_ChinhSach(ref rpilk_csncc);
            cl_grvLookupedit.rpcb_TinhTrangViecLam(ref rpilk_tinhtrangviec);
        }

        void load_lk1()
        {
            cl_Lookupedit.Lucb_Quanhuyen(ref lk1_quan);
        }

        private QuanHeGiaDinh Fill_QuanHeGiaDinh(int machitiet)
        {
            QuanHeGiaDinh gd = new QuanHeGiaDinh();
            gd.Ma_HoNgheo = txt_maho.Text;
            gd.Ma_QuanHe = int.Parse(lk1_quanhevschuho.EditValue.ToString());
            gd.Ten_ThanhVien = txt1_tenchuho.Text;
            gd.NamSinh = int.Parse(txt1_namsinh.Text);
            gd.IsDeleted = false;
            gd.Ma_DanToc = int.Parse(lk1_dantoc.EditValue.ToString());

            if (list_gioitinh.SelectedIndex == 0)
                gd.GioiTinh = true;
            else
                gd.GioiTinh = false;
            if (machitiet != 0)
            {
                gd.Ma_ChiTiet = machitiet;
            }
            ///
            try
            {
                gd.Ma_BaoTroXaHoi = int.Parse(lk1_baotroxh.EditValue.ToString());
            }
            catch
            {
                gd.Ma_BaoTroXaHoi = null;
            }
            ///
            try
            {
                gd.Ma_CapHoc = int.Parse(lk1_capdanghoc.EditValue.ToString());
            }
            catch
            {
                gd.Ma_CapHoc = null;
            }
            ///
            try
            {
                gd.Ma_ChinhSach = int.Parse(lk1_chinhsach.EditValue.ToString());
            }
            catch
            {
                gd.Ma_ChinhSach = null;
            }
            ///
            try
            {
                gd.Ma_ViecLam = int.Parse(lk1_tinhtrangcongviec.EditValue.ToString());
            }
            catch
            {
                gd.Ma_ViecLam = null;
            }
            return gd;

        }

        bool BatLoi_QHGD()
        {
           bool ok = true;
           cl_Lookupedit.BatLoi(lk1_dantoc, "Chưa chọn dân tộc", ref ok);
           cl_Lookupedit.BatLoi(lk1_quanhevschuho, "Chưa chọn quan hệ với chủ hộ", ref ok);
           cl_Lookupedit.BatLoi(txt1_namsinh, "Chưa nhập năm sinh", ref ok);
           cl_Lookupedit.BatLoi(txt1_tenchuho, "Chưa nhập tên chủ hộ", ref ok);
           return ok;
       }

        void XulyButton1()
        {
            if (_NewThanhVien == true)
            {
                bt1_them.Enabled = false;
            }
            else
            {
                bt1_them.Enabled = true;
            }
        }
        #endregion

        #region tab Giải pháp hỗ trợ

        private void bt_save_gpht_Click(object sender, EventArgs e)
        {
            decimal[] money = new decimal[13];
            money[0] = decimal.Parse(txt2_st1.Text);
            money[1] = decimal.Parse(txt2_st2.Text);
            money[2] = decimal.Parse(txt2_st3.Text);
            money[3] = decimal.Parse(txt2_st4.Text);
            money[4] = decimal.Parse(txt2_st5.Text);
            money[5] = decimal.Parse(txt2_st6.Text);
            money[6] = decimal.Parse(txt2_st7.Text);
            money[7] = decimal.Parse(txt2_st8.Text);
            money[8] = decimal.Parse(txt2_st9.Text);
            money[9] = decimal.Parse(txt2_st10.Text);
            money[10] = decimal.Parse(txt2_st11.Text);
            money[11] = decimal.Parse(txt2_st12.Text);
            money[12] = decimal.Parse(txt2_st13.Text);

            string[] notes = new string[13];
            notes[0] = txt2_gc1.Text;
            notes[1] = txt2_gc2.Text;
            notes[2] = txt2_gc3.Text;
            notes[3] = txt2_gc4.Text;
            notes[4] = txt2_gc5.Text;
            notes[5] = txt2_gc6.Text;
            notes[6] = txt2_gc7.Text;
            notes[7] = txt2_gc8.Text;
            notes[8] = txt2_gc9.Text;
            notes[9] = txt2_gc10.Text;
            notes[10] = txt2_gc11.Text;
            notes[11] = txt2_gc12.Text;
            notes[12] = txt2_gc13.Text;
            cl_Insert.ChiTiet_Giaiphaphotro_edit(_IDHoNgheo, money, notes);
            MessageBox.Show("Lưu thông tin thành công");
        }

        private void fill_GPHT(string IDHoNgheo)
        {
            decimal[] money = new decimal[13];
            string[] notes = new string[13];
            cl_Insert.ChiTiet_Giaiphaphotro_show(IDHoNgheo, ref money, ref notes);

            txt2_st1.Text = money[0] + "";
            txt2_st2.Text = money[1] + "";
            txt2_st3.Text = money[2] + "";
            txt2_st4.Text = money[3] + "";
            txt2_st5.Text = money[4] + "";
            txt2_st6.Text = money[5] + "";
            txt2_st7.Text = money[6] + "";
            txt2_st8.Text = money[7] + "";
            txt2_st9.Text = money[8] + "";
            txt2_st10.Text = money[9] + "";
            txt2_st11.Text = money[10] + "";
            txt2_st12.Text = money[11] + "";
            txt2_st13.Text = money[12] + "";

            txt2_gc1.Text = notes[0];
            txt2_gc2.Text = notes[1];
            txt2_gc3.Text = notes[2];
            txt2_gc4.Text = notes[3];
            txt2_gc5.Text = notes[4];
            txt2_gc6.Text = notes[5];
            txt2_gc7.Text = notes[6];
            txt2_gc8.Text = notes[7];
            txt2_gc9.Text = notes[8];
            txt2_gc10.Text = notes[9];
            txt2_gc11.Text = notes[10];
            txt2_gc12.Text = notes[11];
            txt2_gc13.Text = notes[12];
        }

        #endregion

        #region tab Chi tiêu hộ gia đình
        private void bt_save_chitieu_Click(object sender, EventArgs e)
        {
            decimal[] money = new decimal[11];
            money[0] = decimal.Parse(txt3_st1.Text);
            money[1] = decimal.Parse(txt3_st2.Text);
            money[2] = decimal.Parse(txt3_st3.Text);
            money[3] = decimal.Parse(txt3_st4.Text);
            money[4] = decimal.Parse(txt3_st5.Text);
            money[5] = decimal.Parse(txt3_st6.Text);
            money[6] = decimal.Parse(txt3_st7.Text);
            money[7] = decimal.Parse(txt3_st8.Text);
            money[8] = decimal.Parse(txt3_st9.Text);
            money[9] = decimal.Parse(txt3_st10.Text);
            money[10] = decimal.Parse(txt3_st11.Text);

            cl_Insert.ChiTiet_ChiTieuHGD_edit(_IDHoNgheo, money);
            MessageBox.Show("Lưu thông tin chi tiêu thành công !");
            fill_ChiTieu_hgd(_IDHoNgheo);
        }
        private void fill_ChiTieu_hgd(string IDHoNgheo)
        {
            decimal[] money = new decimal[11];
            cl_Insert.ChiTiet_ChiTieuHGD_show(IDHoNgheo, ref money);

            txt3_st1.Text = money[0] + "";
            txt3_st2.Text = money[1] + "";
            txt3_st3.Text = money[2] + "";
            txt3_st4.Text = money[3] + "";
            txt3_st5.Text = money[4] + "";
            txt3_st6.Text = money[5] + "";
            txt3_st7.Text = money[6] + "";
            txt3_st8.Text = money[7] + "";
            txt3_st9.Text = money[8] + "";
            txt3_st10.Text = money[9] + "";
            txt3_st11.Text = money[10] + "";
            decimal s = 0;
            for (int i = 0; i < money.Length; i++)
            {
                s = s + money[i];
            }
            txt3_st12.Text = s + "";
            txt3_st121.Text = txt3_st12.Text;

            // get số lượng thành viên trong hộ gia đình
            List<QuanHeGiaDinh> lgd = iq.QHGD_GetQuanHeGiaDinhByHoNgheoID(_IDHoNgheo);
            int sl = lgd.Count;
            try
            {
                txt3_st122.Text = s / sl + "";
            }
            catch
            {
                txt3_st122.Text = "0";
            }
        }
        #endregion

        #region tab Thu nhập hộ gia đình
        private void bt_save_thunhap_Click(object sender, EventArgs e)
        {
            decimal[] money_tt = new decimal[8];
            money_tt[0] = decimal.Parse(txt4_tt1.Text);
            money_tt[1] = decimal.Parse(txt4_tt2.Text);
            money_tt[2] = decimal.Parse(txt4_tt3.Text);
            money_tt[3] = decimal.Parse(txt4_tt4.Text);
            money_tt[4] = decimal.Parse(txt4_tt5.Text);
            money_tt[5] = decimal.Parse(txt4_tt6.Text);
            money_tt[6] = decimal.Parse(txt4_tt7.Text);
            money_tt[7] = decimal.Parse(txt4_tt8.Text);

            decimal[] money_tc = new decimal[8];
            money_tc[0] = decimal.Parse(txt4_tc1.Text);
            money_tc[1] = decimal.Parse(txt4_tc2.Text);
            money_tc[2] = decimal.Parse(txt4_tc3.Text);
            money_tc[3] = decimal.Parse(txt4_tc4.Text);
            money_tc[4] = decimal.Parse(txt4_tc5.Text);
            money_tc[5] = decimal.Parse(txt4_tc6.Text);

            cl_Insert.ChiTiet_ThuNhapHoGD_edit(_IDHoNgheo, money_tt, money_tc);
            MessageBox.Show("Lưu thông tin thu nhập hộ gia đình thành công");
            fill_Thunhap_hgd(_IDHoNgheo);
        }
        private void fill_Thunhap_hgd(string IDHoNgheo)
        {
            decimal[] money_tt = new decimal[11];
            decimal[] money_tc = new decimal[11];
            cl_Insert.ChiTiet_ThuNhapHoGD_show(IDHoNgheo, ref money_tt, ref money_tc);

            txt4_tt1.Text = money_tt[0] + "";
            txt4_tt2.Text = money_tt[1] + "";
            txt4_tt3.Text = money_tt[2] + "";
            txt4_tt4.Text = money_tt[3] + "";
            txt4_tt5.Text = money_tt[4] + "";
            txt4_tt6.Text = money_tt[5] + "";
            txt4_tt7.Text = money_tt[6] + "";
            txt4_tt8.Text = money_tt[7] + "";

            txt4_tc1.Text = money_tc[0] + "";
            txt4_tc2.Text = money_tc[1] + "";
            txt4_tc3.Text = money_tc[2] + "";
            txt4_tc4.Text = money_tc[3] + "";
            txt4_tc5.Text = money_tc[4] + "";
            txt4_tc6.Text = money_tc[5] + "";
            decimal s_tt = 0;
            for (int i = 0; i < money_tt.Length; i++)
            {
                s_tt = s_tt + money_tt[i];
            }
            decimal s_tc = 0, s_tn = 0;
            for (int i = 0; i < money_tc.Length; i++)
            {
                s_tc = s_tc + money_tc[i];
            }
            s_tn = s_tt - s_tc;

            txt4_tt_1.Text = s_tt + "";
            txt4_tc_1.Text = s_tc + "";
            txt4_tn_1.Text = s_tn + "";
            txt_tn_2.Text = txt4_tn_1.Text;
            txt4_tn1.Text = Cl_Convert.ThuNhap(txt4_tt1.Text, txt4_tc1.Text);
            txt4_tn2.Text = Cl_Convert.ThuNhap(txt4_tt2.Text, txt4_tc2.Text);
            txt4_tn3.Text = Cl_Convert.ThuNhap(txt4_tt3.Text, txt4_tc3.Text);
            txt4_tn4.Text = Cl_Convert.ThuNhap(txt4_tt4.Text, txt4_tc4.Text);
            txt4_tn5.Text = Cl_Convert.ThuNhap(txt4_tt5.Text, txt4_tc5.Text);
            txt4_tn6.Text = Cl_Convert.ThuNhap(txt4_tt6.Text, txt4_tc6.Text);
            txt4_tn7.Text = Cl_Convert.ThuNhap(txt4_tt7.Text, "0");
            txt4_tn8.Text = Cl_Convert.ThuNhap(txt4_tt8.Text, "0");
            //// get số lượng thành viên trong hộ gia đình
            List<QuanHeGiaDinh> lgd = iq.QHGD_GetQuanHeGiaDinhByHoNgheoID(_IDHoNgheo);
            int sl = lgd.Count;
            try
            {
                txt_tn_3.Text = s_tn / sl + "";
            }
            catch
            {
                txt_tn_3.Text = "0";
            }
        }
        private void bt_f5_thunhap_Click(object sender, EventArgs e)
        {
            
        }
        #endregion

        #region tab Tài sản hộ gia đình
        private void bt_save_taisan_Click(object sender, EventArgs e)
        {
            string[] loai = new string[17];
            loai[0] = txt5_l1.Text;
            loai[1] = txt5_l2.Text;
            loai[2] = txt5_l3.Text;
            loai[3] = txt5_l4.Text;
            loai[4] = txt5_l5.Text;
            loai[5] = txt5_l6.Text;
            loai[6] = txt5_l7.Text;
            loai[7] = txt5_l8.Text;
            loai[8] = txt5_l9.Text;
            loai[9] = txt5_l10.Text;
            loai[10] = txt5_l11.Text;
            loai[11] = txt5_l12.Text;
            loai[12] = txt5_l13.Text;
            loai[13] = txt5_l14.Text;
            loai[14] = txt5_l15.Text;
            loai[15] = txt5_l16.Text;
            loai[16] = txt5_l17.Text;

            int[] dientich = new int[17];
            dientich[0] = int.Parse(txt5_sl1.Text);
            dientich[1] = int.Parse(txt5_sl2.Text);
            dientich[2] = int.Parse(txt5_sl3.Text);
            dientich[3] = int.Parse(txt5_sl4.Text);
            dientich[4] = int.Parse(txt5_sl5.Text);
            dientich[5] = int.Parse(txt5_sl6.Text);
            dientich[6] = int.Parse(txt5_sl7.Text);
            dientich[7] = int.Parse(txt5_sl8.Text);
            dientich[8] = int.Parse(txt5_sl9.Text);
            dientich[9] = int.Parse(txt5_sl10.Text);
            dientich[10] = int.Parse(txt5_sl11.Text);
            dientich[11] = int.Parse(txt5_sl12.Text);
            dientich[12] = int.Parse(txt5_sl13.Text);
            dientich[13] = int.Parse(txt5_sl14.Text);
            dientich[14] = int.Parse(txt5_sl15.Text);
            dientich[15] = int.Parse(txt5_sl16.Text);
            dientich[16] = int.Parse(txt5_sl17.Text);

            byte[] sohuu = new byte[17];
            sohuu[0] = byte.Parse(txt5_sh1.Text);
            sohuu[1] = byte.Parse(txt5_sh2.Text);
            sohuu[2] = byte.Parse(txt5_sh3.Text);
            sohuu[3] = byte.Parse(txt5_sh4.Text);
            sohuu[4] = byte.Parse(txt5_sh5.Text);
            sohuu[5] = byte.Parse(txt5_sh6.Text);
            sohuu[6] = byte.Parse(txt5_sh7.Text);
            sohuu[7] = byte.Parse(txt5_sh8.Text);
            sohuu[8] = byte.Parse(txt5_sh9.Text);
            sohuu[9] = byte.Parse(txt5_sh10.Text);
            sohuu[10] = byte.Parse(txt5_sh11.Text);
            sohuu[11] = byte.Parse(txt5_sh12.Text);
            sohuu[12] = byte.Parse(txt5_sh13.Text);
            sohuu[13] = byte.Parse(txt5_sh14.Text);
            sohuu[14] = byte.Parse(txt5_sh15.Text);
            sohuu[15] = byte.Parse(txt5_sh16.Text);
            sohuu[16] = byte.Parse(txt5_sh17.Text);

            decimal[] giatri = new decimal[17];
            #region giatri[0]
            giatri[0] = decimal.Parse(txt5_gt1.Text);
            giatri[1] = decimal.Parse(txt5_gt2.Text);
            giatri[2] = decimal.Parse(txt5_gt3.Text);
            giatri[3] = decimal.Parse(txt5_gt4.Text);
            giatri[4] = decimal.Parse(txt5_gt5.Text);
            giatri[5] = decimal.Parse(txt5_gt6.Text);
            giatri[6] = decimal.Parse(txt5_gt7.Text);
            giatri[7] = decimal.Parse(txt5_gt8.Text);
            giatri[8] = decimal.Parse(txt5_gt9.Text);
            giatri[9] = decimal.Parse(txt5_gt10.Text);
            giatri[10] = decimal.Parse(txt5_gt11.Text);
            giatri[11] = decimal.Parse(txt5_gt12.Text);
            giatri[12] = decimal.Parse(txt5_gt13.Text);
            giatri[13] = decimal.Parse(txt5_gt14.Text);
            giatri[14] = decimal.Parse(txt5_gt15.Text);
            giatri[15] = decimal.Parse(txt5_gt16.Text);
            giatri[16] = decimal.Parse(txt5_gt17.Text);
            #endregion

            cl_Insert.ChiTiet_TaiSanHGD_edit(_IDHoNgheo, loai, dientich, sohuu, giatri);
            MessageBox.Show("Lưu Thông tin chi tiết tài sản thành công ");
        }
        private void fill_TaiSan_hgd(string IDHoNgheo)
        {
            string[] loai = new string[17];
            int[] dientich = new int[17];
            byte[] sohuu = new byte[17];
            decimal[] giatri = new decimal[17];
            cl_Insert.ChiTiet_TaiSanHGD_show(IDHoNgheo, ref loai, ref dientich, ref sohuu, ref giatri);

            txt5_l1.Text = loai[0] + "";
            txt5_l2.Text = loai[1] + "";
            txt5_l3.Text = loai[2] + "";
            txt5_l4.Text = loai[3] + "";
            txt5_l5.Text = loai[4] + "";
            txt5_l6.Text = loai[5] + "";
            txt5_l7.Text = loai[6] + "";
            txt5_l8.Text = loai[7] + "";
            txt5_l9.Text = loai[8] + "";
            txt5_l10.Text = loai[9] + "";
            txt5_l11.Text = loai[10] + "";
            txt5_l12.Text = loai[11] + "";
            txt5_l13.Text = loai[12] + "";
            txt5_l14.Text = loai[13] + "";
            txt5_l15.Text = loai[14] + "";
            txt5_l16.Text = loai[15] + "";
            txt5_l17.Text = loai[16] + "";


            txt5_sl1.Text = dientich[0] + "";
            txt5_sl2.Text = dientich[1] + "";
            txt5_sl3.Text = dientich[2] + "";
            txt5_sl4.Text = dientich[3] + "";
            txt5_sl5.Text = dientich[4] + "";
            txt5_sl6.Text = dientich[5] + "";
            txt5_sl7.Text = dientich[6] + "";
            txt5_sl8.Text = dientich[7] + "";
            txt5_sl9.Text = dientich[8] + "";
            txt5_sl10.Text = dientich[9] + "";
            txt5_sl11.Text = dientich[10] + "";
            txt5_sl12.Text = dientich[11] + "";
            txt5_sl13.Text = dientich[12] + "";
            txt5_sl14.Text = dientich[13] + "";
            txt5_sl15.Text = dientich[14] + "";
            txt5_sl16.Text = dientich[15] + "";
            txt5_sl17.Text = dientich[16] + "";

            txt5_sh1.Text = sohuu[0] + "";
            txt5_sh2.Text = sohuu[1] + "";
            txt5_sh3.Text = sohuu[2] + "";
            txt5_sh4.Text = sohuu[3] + "";
            txt5_sh5.Text = sohuu[4] + "";
            txt5_sh6.Text = sohuu[5] + "";
            txt5_sh7.Text = sohuu[6] + "";
            txt5_sh8.Text = sohuu[7] + "";
            txt5_sh9.Text = sohuu[8] + "";
            txt5_sh10.Text = sohuu[9] + "";
            txt5_sh11.Text = sohuu[10] + "";
            txt5_sh12.Text = sohuu[11] + "";
            txt5_sh13.Text = sohuu[12] + "";
            txt5_sh14.Text = sohuu[13] + "";
            txt5_sh15.Text = sohuu[14] + "";
            txt5_sh16.Text = sohuu[15] + "";
            txt5_sh17.Text = sohuu[16] + "";

            txt5_gt1.Text = giatri[0] + "";
            txt5_gt2.Text = giatri[1] + "";
            txt5_gt3.Text = giatri[2] + "";
            txt5_gt4.Text = giatri[3] + "";
            txt5_gt5.Text = giatri[4] + "";
            txt5_gt6.Text = giatri[5] + "";
            txt5_gt7.Text = giatri[6] + "";
            txt5_gt8.Text = giatri[7] + "";
            txt5_gt9.Text = giatri[8] + "";
            txt5_gt10.Text = giatri[9] + "";
            txt5_gt11.Text = giatri[10] + "";
            txt5_gt12.Text = giatri[11] + "";
            txt5_gt13.Text = giatri[12] + "";
            txt5_gt14.Text = giatri[13] + "";
            txt5_gt15.Text = giatri[14] + "";
            txt5_gt16.Text = giatri[15] + "";
            txt5_gt17.Text = giatri[16] + "";
        }
        #endregion

        #region tab Thông tin khác
        private void bt_save_khac_Click(object sender, EventArgs e)
        {
            try
            {
                iq = QueryMgr.GetDbInstance();
                iq.Ctnvkp_DeleteChiTiet_NguyenVongKhacPhucByHoNgheo(txt_maho.Text);
                iq.Ctnntn_Delete_ChiTiet_NguyenNhanThuNhapByHoNgheo(txt_maho.Text);
                for (int i = 0; i < checklist_1.ItemCount; i++)
                {
                    if (checklist_1.GetItemChecked(i))
                    {
                        ChiTiet_NguyenNhanThuNhap nn = new ChiTiet_NguyenNhanThuNhap();
                        nn.IsDeleted = false;
                        nn.Ma_HoNgheo = txt_maho.Text;

                        int gtri = int.Parse(checklist_1.GetItemValue(i).ToString());
                        nn.Ma_NguyenNhan = gtri;
                        iq.Ctnntn_NewChiTiet_NguyenNhanThuNhap(nn);
                    }
                }


                for (int i = 0; i < checklist_2.ItemCount; i++)
                {
                    if (checklist_1.GetItemChecked(i))
                    {
                        ChiTiet_NguyenVongKhacPhuc nv = new ChiTiet_NguyenVongKhacPhuc();
                        nv.IsDeleted = false;
                        nv.Ma_HoNgheo = txt_maho.Text;

                        int gtri = int.Parse(checklist_2.GetItemValue(i).ToString());
                        nv.Ma_NguyenVong = gtri;
                        iq.Ctnvkp_NewChiTiet_NguyenVongKhacPhuc(nv);
                    }
                }
                MessageBox.Show("Lưu Thông tin thành công");
            }
            catch { }
        }
        decimal _Tongthunhap = 0;
        void set_checklist_TTKhac()
        {
            List<ChiTiet_NguyenNhanThuNhap> nntn = iq.CTNNTN_GetChiTiet_NguyenNhanThuNhapByIDHoNgheo(txt_maho.Text);
            List<ChiTiet_NguyenVongKhacPhuc> nvkp = iq.CTNVKP_GetChiTiet_NguyenVongKhacPhucByIDHoNgheo(txt_maho.Text);
            for (int i = 0; i < nntn.Count; i++)
            {
                for(int j = 0;j<checklist_1.Items.Count;j++)
                {
                    if (nntn[i].Ma_NguyenNhan == int.Parse(checklist_1.Items[j].Value.ToString()))
                    {
                        checklist_1.Items[j].CheckState = CheckState.Checked;
                        break;
                    }
                }
            }

            for (int i = 0; i < nvkp.Count; i++)
            {
                for (int j = 0; j < checklist_2.Items.Count; j++)
                {
                    if (nvkp[i].Ma_NguyenVong == int.Parse(checklist_2.Items[j].Value.ToString()))
                    {
                        checklist_2.Items[j].CheckState = CheckState.Checked;
                        break;
                    }
                }
            }
            ///----------------------------------------------------------------///
            if (txt_tn_2.Text == "")
            {
                checklist_3.Items[0].CheckState = CheckState.Checked;
            }
            else
            {
                _Tongthunhap = decimal.Parse(txt_tn_2.Text);
                int iCheck = Cl_Convert.getLoaiByTongThuNhap(_Tongthunhap);
                checklist_3.Items[iCheck - 1].CheckState = CheckState.Checked;
                if (_Tongthunhap <= 700)
                    checklist_4.Items[0].CheckState = CheckState.Checked;
                else
                    checklist_4.Items[1].CheckState = CheckState.Checked;

            }
        }
        #endregion 

        private void lk1_phuong_EditValueChanged(object sender, EventArgs e)
        {
            
        }

        private void lk1_phuong_EditValueChanging(object sender, ChangingEventArgs e)
        {
            //((PhuongXa)avProduct.GetFocusedRow()).Category = LuanVanDBDataContext.Categories.Single(c => c.CategoryID == (int)e.NewValue);
            //LuanVanDBDataContext.Categories.Single(c => c.CategoryID == (int)e.NewValue);
            //LuanVanDBDataContext DB = new LuanVanDBDataContext();
            //PhuongXa px = new PhuongXa();
            //px = DB.PhuongXas.Single(c => c.Ma_Phuong == (int)e.NewValue);
        }
        
    }
}
